<?
include ("../config.php");
include("../include/classes/mysql.php");

$db = new sql_db($sugar_config["dbconfig"]["db_host_name"],$sugar_config["dbconfig"]["db_user_name"],$sugar_config["dbconfig"]["db_password"],$sugar_config["dbconfig"]["db_name"]);
$db->sql_query("SET NAMES latin1");
$fields = array(
array("c","first_name"),
array("c","last_name"),
array("c","title"),
array("a","name"),
array("a","account_type"),
array("a","billing_address_street"),
array("a","billing_address_city"),
array("a","billing_address_state"),
array("a","shipping_address_postalcode"),
array("a","shipping_address_street"),
array("a","shipping_address_city"),
array("a","shipping_address_state"),
array("a","shipping_address_postalcode"),
array("a","phone_office"),
array("a","phone_fax"),
array("a","sic_code","Vergi Numarası"),
array("a","rating","Vergi Dairesi"),
array("ea","email_address"),
//array("c","email1","Contact E-mail"),
array("c","primary_address_street"),
array("c","primary_address_city"),
array("c","primary_address_state"),
array("c","primary_address_postalcode"),
array("c","primary_address_country"),
array("c","phone_home"),
array("c","phone_mobile"),
array("c","phone_fax"),

);
if(!$_POST["targetListID"]) {
	$sql = "SELECT p.id,p.name FROM prospect_lists AS p WHERE p.deleted ='0' ORDER BY p.name ASC"; //Target lists
	$db->sql_query($sql);
	?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Export a Target List</title>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<b>Please Select a target list:</b><br>
<select name="targetListID">
<? while(list($targetID,$targetName) = $db->sql_fetchrow($db->query_result)) {?>
<option value="<?=$targetID?>"><?=$targetName?></option>
<? } ?>
</select>
<input type="submit" value="Get List">
</form>
<p>If you have trouble, please add an issue to <a href="http://code.google.com/p/targetlist-export-for-sugarcrm/" target="_blank">http://code.google.com/p/targetlist-export-for-sugarcrm/</a>.</p>
</body> 
</html>
<?
} else {
	foreach($fields AS $f){
		$targetColumns .= $f[0].".".$f[1].",";
	}
$targetColumns = substr($targetColumns,0,-1); // Selected Columns
	 
	$sql = "SELECT $targetColumns FROM
`prospect_lists_prospects` AS `plp` ,
`contacts` AS `c`
Left Outer Join `accounts_contacts` AS `ac` ON `c`.`id` = `ac`.`contact_id` AND `ac`.`deleted` =  '0' 
Left Outer Join `accounts` AS `a` ON `a`.`id` = `ac`.`account_id` AND  `a`.`deleted` =  '0'
Left Outer Join `email_addr_bean_rel` AS `eabr` ON `eabr`.`bean_module` ='Contacts' AND `eabr`.`primary_address` ='1' AND `eabr`.`bean_id` = `c`.`id` AND `eabr`.`deleted` = '0'
Left Outer Join `email_addresses` AS `ea` ON `ea`.`id` = `eabr`.`email_address_id` AND `ea`.`deleted` = '0'
WHERE
`plp`.`prospect_list_id` =  '".$_POST["targetListID"]."' AND
`plp`.`related_type` =  'Contacts' AND
`plp`.`related_id` =  `c`.`id` AND
`c`.`deleted` =  '0' AND
`plp`.`deleted` =  '0'
GROUP BY
`c`.`id`
ORDER BY 
`a`.`billing_address_street` ASC , `c`.`primary_address_street` ASC";
	//print $sql;
$db->sql_query($sql);
$rowNumber = 2;
foreach($fields AS $f) {
	$line .= "\"".$f[1]."\";";
}
$lines .= $line."\n";
$line = null;
while($row = $db->sql_fetchrow($db->query_result)) {
	for($i=0;$i<count($fields);$i++) {
		if($row[$i] =='&nbsp') {
			$line .="\t";
		} else {
		$line .= eregi_replace("\r\n","","\"".$row[$i])."\";";
		}
	}
	$lines .= $line."\n\r";
	$line = null;
}

header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=targetlist.txt"); 
header("Pragma: no-cache");
header("Expires: 0");  

print $lines;
}
?>
